#ifndef REGION_READER_H
#define REGION_READER_H

#include "reader.h"
#include <vector>
#include <map>
#include <sstream>

#define NUM_REGION_SIZE		sizeof(int)
#define	REGION_NAME_SIZE	(sizeof(char) * 20)
#define REGION_INDEX_SIZE	sizeof(long)
#define REGION_INFO_HEADER_SIZE (REGION_NAME_SIZE+REGION_INDEX_SIZE)

#define NUM_REGION_OFFSET	6 * sizeof(int)
#define REGION_INFO_HEADER_OFFSET (NUM_REGION_OFFSET + NUM_REGION_SIZE)
#define MAX_REGION_VAR 26

#define NUM_SUMMARY_DATA 15

#define GET_REGION_VAR_SQL  "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Hour Var Selection ARZ'"

#define GET_REGION_ANC_INDEX_SQL "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Ancillary Service Type Index'"

#define GET_REGION_EMISSION_COUNT_SQL "SELECT COUNT(MONITOR)AS NUM_EMI FROM EMISSIONTYPE WHERE MONITOR=-1"

#define GET_REGION_EMISSION_NAME_SQL "SELECT NAME FROM EMISSIONTYPE WHERE MONITOR=-1" //Yanli Wei, 06/10/2010

#define GET_REGION_EMISSION_SETTING_SQL "SELECT VALUE FROM SIMULATIONCONTROL WHERE NAME='Emission Model Option'"



typedef struct _Region_info 
{
	long Region_index;
	char RegionName[20];
}Region_info;

// Reorderd annd add last three variables to match the OUTPUT binary file, Yanli Wei, 06/14/2010
typedef struct _Region_yearly_data
{
	float Avg_LMP;
	float Avg_LMP_Congestion;
	float Avg_LMP_Loss;
	float Tot_Generation;
	float Tot_Load;
	float Tot_ServedLoad;
	float Tot_UnservedLoad;
	float Tot_Pumping;
	float Tot_LoadPayment;
	float Tot_Revenue;
	float Tot_GenCost;
	float Tot_UnservedCost;
	float Tot_CongDemandPay;
	float Tot_LossDemandPay;
	float Tot_CongSupplyRev;
}Region_yearly_data;

/*
typedef struct _stats
{
	float total;
	float avg;
	float min;
	float max;
}stats;
*/

class region_reader: public reader
{

protected:

	int summary_file_Handle;					// file handlder for summary binary file
	long hourly_num_Region;						// total number of Region store in the hourly binary file
	CString Region_var_setting;					// Region variable output bit setting
	CString Region_anc_setting;					// Region Ancillary Service setting
	CString Region_emssions_count;				// Region emssions Count setting
	CString Region_emssions_option;				// Region option setting

	map <CString, int> hourly_Region_index_map;	// hourly Regionerators' name and index map
	map <CString, int> var_index_map;			// hourly Region varables name and index map

	/* read Region varables setting from MS DB*/
	void read_Region_var_setting(CString * Region_var_setting);
	/* read Region info from binary file (hourly, summary)*/
	void read_Region_info(int f_handler, long * num, vector<Region_info> * info_list, map <CString, int> * info_map);
	/* read number of Region from binary file (hourly, summary)*/
	void read_Region_num(int f_handle, long * num);
	/* read emission types */
	void readEmissionType(); // Yanli Wei, 06/10/2010


public:

	int num_ancs;								// total number of Ancillary Service
	int num_emissions;							// total number of emissions
	int num_vars;								// total number of selected output Region variables
	int emissions_option;                       // Emission option, Yanli Wei, 06/10/2010

	vector<char *> Region_var_list;				// Region variables list
	vector<Region_info> hourly_Region_info_list;	// Region information list 
	vector<CString> Region_type_list;				// Region types and index map
	map <CString, int> Region_type_map;			// Region types and index map

	vector<CString> emission_type_list;         // Emission type list, Yanli Wei, 06/10/2010     
	vector<CString> AS_type_list;				// Ancillary service type list, Yanli Wei, 06/10/2010

	/* constructors and deconstructor */
	region_reader();
	region_reader(char *HourlyBinFile, char *SummaryBinFile, char *dbFile);
	~region_reader();

	char ** GET_REGION_var_list();
	int get_var_list_size();
	int GET_REGION_info_list_size();
	int GET_REGION_var_list_size();
	int GET_REGION_index(char *, map <CString, int> * );
	long get_num_Region();
	unsigned long get_hourly_var_offset(int Region_index, unsigned long hour, int var_index);
	unsigned long get_summary_data_offset(int Region_index);

	float GET_REGION_var(char* RegionName, char * var_name, unsigned long hour);
	float * getAllHourlyVal(char* RegionName, unsigned long Region_hrs);

	Region_info * GET_REGION_info_list();
	Region_yearly_data GET_REGION_yearly_data(CString RegionName);

	void readAncSetting(CString);
	void readEmssionsOption(CString area_emssions_option); // Yanli Wei, 06/10/2010
	void readEmssionsCount(CString area_emssions_count); // Yanli Wei, 06/10/2010


};

#endif